package com.Servlet;

import com.dao.DaoFactory;
import com.entity.book_examine;
import com.entity.reader;
import com.entity.search;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class newbookServlet  extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setHeader("Cache-control", "no-cache");
        request.setCharacterEncoding("UTF-8");

        String bookname = request.getParameter("bookname");
        String author_name = request.getParameter("author_name");
        String types = request.getParameter("types");
        String brief_introduction = request.getParameter("brief_introduction");

        System.out.println("------------- 新书注册--------------------");
        System.out.println("bookname:"+bookname);
        System.out.println("author_name:"+author_name);
        System.out.println("types:"+types);
        System.out.println("brief_introduction:"+brief_introduction);
        System.out.println("-----------------------------------------");

        book_examine newbook = new book_examine();
        newbook.setBookname(bookname);
        newbook.setAuthor_name(author_name);
        newbook.setTypes(types);
        newbook.setBrief_introduction(brief_introduction);


        try {
            search check_book = DaoFactory.getInstance().getsearchDao().findBybookname(bookname);
            book_examine check_book2 = DaoFactory.getInstance().getbook_examineDao().findbybookname(bookname);

            if(check_book == null && check_book2 == null){
                //执行跳转
                DaoFactory.getInstance().getbook_examineDao().add(newbook);
                //执行跳转
                System.out.println("新书创建成功,等待审核");
//                session.setAttribute("user", entity);
                request.getRequestDispatcher("newbook_success.jsp").forward(request,response);
            }
            else{
                //返回给用户“错误”
                System.out.println("错误，书名已存在");
                request.setAttribute("booknameerror",1);
                request.getRequestDispatcher("newbook.jsp").forward(request,response);

            }


        } catch (SQLException e) {
            e.printStackTrace();
        }






    }

}

